﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace AndersLiu.Bambook.Sdk.TestApp
{
    class Program
    {
        int hConn = 0;

        static void Main(string[] args)
        {
            Program p = new Program();
            //p.TestGetSDKVersion();
            //p.TestConnect();
            //p.TestDisconnect();
            //p.TestGetConnectStatus();
            //p.TestGetDeviceInfo();
            //p.TestGetPrivateBookInfo();
            //p.TestFetchPrivateBook();
            p.TestUnpackFileFromSnb();
            //p.TestGetErrorString();

#if DEBUG
            Console.WriteLine();
            Console.Write("Finished.");
            Console.ReadKey(true);
#endif
        }

        void TestGetSDKVersion()
        {
            Console.WriteLine("======== TestGetSDKVersion ========");

            int version;
            var r = Bambook.GetSDKVersion(out version);
            Console.WriteLine("Result: {0}, SDK Version: 0x{1:X8}", r, version);
        }

        void TestConnect()
        {
            Console.WriteLine("======== TestConnect ========");

            var r = Bambook.Connect(Bambook.DEFAULT_BAMBOOK_IP, 0, out hConn);
            Console.WriteLine("Result: {0}, hConn: 0x{1:X8}", r, hConn);
        }

        void TestDisconnect()
        {
            Console.WriteLine("======== TestDisconnect ========");

            var r = Bambook.Disconnect(hConn);
            Console.WriteLine("Result: {0}", r);
        }

        void TestGetConnectStatus()
        {
            Console.WriteLine("======== TestGetConnectStatus ========");

            Bambook.ConnectionState status;
            var r = Bambook.GetConnectStatus(hConn, out status);
            Console.WriteLine("Result: {0}, Status: {1}", r, status);
        }

        void TestGetDeviceInfo()
        {
            Console.WriteLine("======== TestGetDeviceInfo ========");

            Bambook.DeviceInfo info = new Bambook.DeviceInfo();
            info.SizeInBytes = Marshal.SizeOf(info);
            var r = Bambook.GetDeviceInfo(hConn, ref info);
            Console.WriteLine("Result: {0}", r);
            Console.WriteLine("DeviceInfo.SizeInBytes = {0}", info.SizeInBytes);
            Console.WriteLine("DeviceInfo.SerialNumber = {0}", info.SerialNumber);
            Console.WriteLine("DeviceInfo.FirmwareVersoin = {0}", info.FirmwareVersoin);
            Console.WriteLine("DeviceInfo.DeviceVolume = {0}", info.DeviceVolume);
            Console.WriteLine("DeviceInfo.SpareVolume = {0}", info.SpareVolume);
        }

        void TestGetPrivateBookInfo()
        {
            Console.WriteLine("======== TestGetPrivateBookInfo ========");

            Bambook.PrivateBookInfo info = new Bambook.PrivateBookInfo();
            info.SizeInBytes = Marshal.SizeOf(info);
            for (var r = Bambook.GetFirstPrivateBookInfo(hConn, ref info);
                r == Bambook.Result.Success;
                r = Bambook.GetNextPrivateBookInfo(hConn, ref info))
            {
                Console.WriteLine("----------------");
                Console.WriteLine("SizeInBytes = {0}", info.SizeInBytes);
                Console.WriteLine("Guid = {0}", info.Guid);
                Console.WriteLine("Name = {0}", info.Name);
                Console.WriteLine("Author = {0}", info.Author);
                Console.WriteLine("Abstract = {0}", info.Abstract);
            }
        }

        // Failed.
        void TestFetchPrivateBook()
        {
            Console.WriteLine("======== TestFetchPrivateBook ========");

            AutoResetEvent EV = new AutoResetEvent(false);
            Bambook.TransferCallback cb = (Bambook.TransferState status, int progress, int userData) =>
            {
                Console.WriteLine("Transferring: {0}, {1}, {2}", status, progress, userData);
                if (status == Bambook.TransferState.Done)
                    EV.Set();
            };

            var r = Bambook.FetchPrivateBook(hConn, "e0bgesaerwang.snb", @"C:\Bambook_Temp\", cb, 0);
            EV.WaitOne();
            //Thread.Sleep(30000);
            Console.WriteLine("Result: {0}", r);
        }

        void TestUnpackFileFromSnb()
        {
            var r = Bambook.UnpackFileFromSnb(@"D:\Temp\bb\道德经.snb", "snbf/book.snbf", @"d:\Temp\bb\book.snbf");
            Console.WriteLine("Result: {0}", r);
        }

        void TestGetErrorString()
        {
            Console.WriteLine("======== TestGetErrorString ========");

            Console.WriteLine("{0}: {1}", Bambook.Result.Success, Bambook.GetErrorString(Bambook.Result.Success));
            Console.WriteLine("{0}: {1}", Bambook.Result.Fail, Bambook.GetErrorString(Bambook.Result.Fail));
            Console.WriteLine("{0}: {1}", Bambook.Result.NotImplemented, Bambook.GetErrorString(Bambook.Result.NotImplemented));
            Console.WriteLine("{0}: {1}", Bambook.Result.Disconnected, Bambook.GetErrorString(Bambook.Result.Disconnected));
            Console.WriteLine("{0}: {1}", Bambook.Result.ParameterError, Bambook.GetErrorString(Bambook.Result.ParameterError));
            Console.WriteLine("{0}: {1}", Bambook.Result.Timeout, Bambook.GetErrorString(Bambook.Result.Timeout));
            Console.WriteLine("{0}: {1}", Bambook.Result.InvalidHandle, Bambook.GetErrorString(Bambook.Result.InvalidHandle));
            Console.WriteLine("{0}: {1}", Bambook.Result.InvalidFile, Bambook.GetErrorString(Bambook.Result.InvalidFile));
            Console.WriteLine("{0}: {1}", Bambook.Result.InvalidDirectory, Bambook.GetErrorString(Bambook.Result.InvalidDirectory));
            Console.WriteLine("{0}: {1}", Bambook.Result.Busy, Bambook.GetErrorString(Bambook.Result.Busy));
            Console.WriteLine("{0}: {1}", Bambook.Result.EndOfFile, Bambook.GetErrorString(Bambook.Result.EndOfFile));
            Console.WriteLine("{0}: {1}", Bambook.Result.IOError, Bambook.GetErrorString(Bambook.Result.IOError));
            Console.WriteLine("{0}: {1}", Bambook.Result.FileNotInside, Bambook.GetErrorString(Bambook.Result.FileNotInside));

            Console.WriteLine("{0}: {1}", (Bambook.Result)123, Bambook.GetErrorString((Bambook.Result)123));
        }
    }
}


// TODO:
// AddPrivateBook
// ReplacePrivateBook
// DeletePrivateBook